package com.zx.controller;

import com.github.pagehelper.PageInfo;
import com.zx.bean.Bill;
import com.zx.bean.BillType;
import com.zx.service.BillService;
import com.zx.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author CaesarChang张旭
 * @Date 2020/11/11  10:38 上午
 * @Version 1.0
 */
@Controller
@RequestMapping("/bill")
public class BillController {
    @Resource
    private TypeService typeService;
    @Autowired
    private BillService billService;

    @RequestMapping("/list")
    public String list(Bill b, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        List<Bill> list = billService.list(b);
        model.addAttribute("bills", list);
        return "/bill/list";
    }

    /*** 调到添加页面 * @return */
    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        return "/bill/add";
    }

    /*** 添加 * @param b * @return */
    @RequestMapping("/add")
    public String add(Bill b) {
        billService.add(b);
        return "redirect:/bill/list-page";
    }


    /*** 删除 * @param id * @return */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        billService.delete(id);
        return "redirect:/bill/list-page";
    }

    /*** 修改 * @param id * @return */
    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Long id, Model model) {
        List<BillType> types = typeService.list();
        model.addAttribute("types", types);
        Bill bill = billService.get(id);
        model.addAttribute("bill", bill);
        return "/bill/update";
    }

    /*** 修改 * @param b * @return */
    @RequestMapping("/update")
    public String update(Bill b) {
        billService.update(b);
        return "redirect:/bill/list-page";
    }

    /**
     * 分页方法
     */

    @RequestMapping("/list-page")
    public String listPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            Bill b,
            Model model
    ) {
        List<BillType> types = typeService.list();

        model.addAttribute("types", types);
        model.addAttribute("bill", b);

        PageInfo<Bill> pageInfo = billService.listPage(b, pageNum, pageSize);
        model.addAttribute("page", pageInfo);


        return "/bill/list-page";

    }


}
